import {
  queryRuleList,
  addRuleInfo,
  updateRuleInfo,
  activeRule
} from '@/services/api';

export default {
  namespace: 'rule',

  state: {
    ruleList: [],
  },

  effects: {
    *fetchQueryRuleList({ payload, callback }, { call, put }) {
      const response = yield call(queryRuleList, payload);
      if (callback) callback(response);
      yield put({
        type: 'setRuleList',
        payload: response,
      });
    },
    *fetchAddRuleInfo({ payload, callback }, { call, put }) {
      const response = yield call(addRuleInfo, payload);
      if (callback) callback(response);
    },
    *fetchUpdateRuleInfo({ payload, callback }, { call, put }) {
      const response = yield call(updateRuleInfo, payload);
      if (callback) callback(response);
    },
    *fetchActiveRule({ payload, callback }, { call, put }) {
      const response = yield call(activeRule, payload);
      if (callback) callback(response);
    },
  },

  reducers: {
    setRuleList(state, action) {
      return {
        ...state,
        ruleList: action.payload,
      };
    }
  },
};
